
global name "C0_cash_did_prepare"
cap log close
log using "${pfile}/${name}.smcl", replace smcl


// Defining final sample
use A0_cash_base, clear

keep if year >= 2006 & year <= 2014 // can use more years of data
keep if yf_min >= 2002 & yf_min <= 2016

keep if abs(x)<=0.8
drop if x==0	
keep if wkd >= 31  // Abono eligibility (1)

drop if openjob==1

// Range
drop if dt_dism < mdy(6,1,year)
drop if dt_dism > mdy(7,15,year)

keep if tpv == 10
drop if natjuridica <= 2038
keep if hor >= 30
drop if ind == 1

g age_dism = (dt_dism-dtb)/365
xtile qt_dism = age_dism, n(10)
replace qt_dism = 99 if age_dism == . // age at dismissal is not available for 2011 and 2012

replace school = school * 100
replace school = 0 if school == .
replace race = race + 10

g elig = x <=0

g s3 = und <= 12
keep s3 und elig cpf pis id year yf_min yf_max mindt_hir maxdt_hir dt_dism x tempempr school ind qt_dism race gend hor uf remmedr ocup mun p_causadesli p_tempempr p_id // 

save C0_cash_did, replace


do "${dbox}Brazil_Cash_on_new/Do_restat/C0_cash_did_prepare_additionalvars.do"


// *** // FINAL DATASET CASH GRANT EFFECTS - DID ANALYSIS // *** //
use C0_cash_did, clear

keep if year>=2011 & year<=2014
keep if yf_min>=2006 & yf_max<=2010
drop if year < yf_min

keep if abs(x)<=.8
keep if yf_min==yf_max
*keep if mindt_hir == maxdt_hir

g y5 = year>=yf_min+5

merge 1:1 year id using C0_additional, keep(1 3) nogen
replace n_und = (mdy(12,31,2017) - dt_dism)/7 if n_und==.
replace und = n_und
drop n_und

replace mon12 = 0 if mon12==.
replace mon24 = 0 if mon24==.
replace mon36 = 0 if mon36==.
replace inc12 = 0 if inc12==.
replace inc24 = 0 if inc24==.
replace inc36 = 0 if inc36==.

g emp12 = mon12>0 & mon12!=.
g emp24 = mon24>0 & mon24!=.
g emp36 = mon36>0 & mon36!=.

g totmon = mon12+mon24+mon36
g totinc = inc12+inc24+inc36

g totwage = totinc/totmon
g wage12 = inc12/mon12
g wage24 = inc24/mon24
g wage36 = inc36/mon36

foreach x in 3 6 36 { // everything monthly
*g haz`x' = und<=30/7 * `x' if und >= 30/7 * (`x'-1)
g sur`x' = und > 30/7 * (`x')
g dur`x' = und
replace dur`x' = 30/7 * `x' if und >= 30/7 * `x'
}

save C_cash_did, replace



